from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from rest_framework.decorators import api_view
import logging
import json

logger = logging.getLogger('log')

from ..serverEvCfg import httpResponseSignal
# 导入所需的models
from ..models import ptTestHeards,ptTestPublicInterface
from ..dataBaseSQL.mysqlDB import getSql, getmoreSql
from ..serverEvCfg.sqlXmlOperate import get_sql
from ..utilService import exeTestCase
from django.core import serializers

@csrf_exempt  # 允许跨域访问
@api_view(['POST'])
def addPublicInterface(request):
    '''
    新增公共接口
    '''
    data = json.loads(request.body)
    try:
        mmhpubIn = ptTestPublicInterface.objects.create(url=data['url'], method=data['method'],
                                                             request_data_type=data['request_data_type'],
                                                             request_header_param=data['request_header_param'],
                                                             request_data_content=data['request_data_content'],
                                                             request_value=data['request_value'])

        mmhherad = ptTestHeards.objects.create(headrsName=data["headrsName"], tpi_id=mmhpubIn.request_id)
        mmhherad.save()
        mmhpubIn.save()
        response = httpResponseSignal.httpResponseSignal().getSuccess()
        return JsonResponse(response)

    except  EOFError as e:
        logging.error(e)
        response = httpResponseSignal.httpResponseSignal().getErorr()
        return JsonResponse(response)


path = "interfacs.xml"
@csrf_exempt  # 允许跨域访问
@api_view(['POST'])
def queryPublicInterface(request):
    '''
    查询公共接口接口管理
    '''
    try:
        sql = get_sql("Case", "pubInter", "queryPubInter", sqlname=path, fileName="wzyImplementService")
        pubInList = getmoreSql(sql)
        response = httpResponseSignal.httpResponseSignal().getSuccess()
        response["data"] = pubInList
        return JsonResponse(response)
    except EOFError as e:
        logging.error(e)

@csrf_exempt  # 允许跨域访问
@api_view(['POST'])
def getPublicInterface(request):
    '''
    查询公共接口接口管理
    '''
    data = json.loads(request.body)
    try:
        print(data["id"])
        dataTXT = ptTestPublicInterface.objects.filter(request_id=data["id"])
        print(type(dataTXT.values()))



        response = httpResponseSignal.httpResponseSignal().getSuccess()
        response["data"] = dataTXT
        return JsonResponse(response)
    except EOFError as e:
        logging.error(e)

@csrf_exempt  # 允许跨域访问
@api_view(['POST'])
def runPublicInterface(request):
    '''
    运行公共接口管理
    '''
    data = json.loads(request.body)
    try:

        exeTestCase.exeTestCase().runRepositoryPublicMethod(id=data["id"])
        response = httpResponseSignal.httpResponseSignal().getSuccess()
        return JsonResponse(response)
    except EOFError as e:
        logging.error(e)


@csrf_exempt  # 允许跨域访问
@api_view(['POST'])
def delPublicInterface(request):  # 删除共接口接口管理
    data = json.loads(request.body)
    try:
        ptTestPublicInterface.objects.get(request_id=data["id"]).delete()
        ptTestHeards.objects.get(tpi_id=data["id"]).delete()
        response = httpResponseSignal.httpResponseSignal().getSuccess()
        return JsonResponse(response)
    except EOFError as e:
        logging.error(e)

